package edu.fub.pub2search.app;

import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * This class is used to manage the layout of the user screen map. It also
 * includes how the overlays are stored to the map.
 * 
 * @author Andrej
 */
public class MapViewer extends LinearLayout {
	
	/** Overlays */
	private PubOverlay pubOverlay;
	private PersonOverlay personOverlay;
	private InfoOverlay infoOverlay;
	
	/** List of pub items that are shown in the map */
	private List<OverlayItem> pubItems;
	
	/** MapView object */
	private MapView mapView;
		
	
	/**
	 * The constructor.
	 * 
	 * @param context The application context.
	 */
	public MapViewer(Context context) {
		super(context);
		init();
	}
	
	/**
	 * The constructor.
	 * 
	 * @param context The application context.
	 * @param attrs The attributes to build the layout parameters from
	 */
	public MapViewer(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	/**
	 * Set the pub overlay.
	 * 
	 * @param pubItems The pub locations.
	 */
	public void setPubLocations(List<OverlayItem> pubItems) { 

		// Clean the previous person location, if any.
		List<Overlay> overlays = mapView.getOverlays();
    	if (overlays.contains(pubOverlay)) {
			int index = overlays.indexOf(pubOverlay);
	    	overlays.remove(index);
    	}
    	
		// Setup the pub overlay
		this.pubItems = pubItems;
		pubOverlay = new PubOverlay(this);
		
		// Add to the map.
		overlays.add(pubOverlay);
	}
	
	/**
	 * Get the current pub locations.
	 * 
	 * @return The pub locations.
	 */
	public List<OverlayItem> getPubLocations() {
		
		return pubItems;
	}

	/**
	 * Set the person overlay.
	 * 
	 * @param personPoint The person location.
	 * @param radius The searching radius.
	 */
	public void setPersonLocation(GeoPoint personGeoPoint, int radius) {

		// Clean the previous person location, if any.
		List<Overlay> overlays = mapView.getOverlays();
    	if (overlays.contains(personOverlay)) {
			int index = overlays.indexOf(personOverlay);
	    	overlays.remove(index);
    	}
		
    	// Setup the person overlay
		personOverlay = new PersonOverlay(personGeoPoint, radius, this);

		// Add to the map
		overlays.add(personOverlay);
	}
	
	/**
	 * Set the infoStatus overlay.
	 * 
	 * @param infoStatus Info status string
	 */
	public void setInfoStatus(String infoStatus) {
		
		// Clean the previous info status, if any.
		List<Overlay> overlays = mapView.getOverlays();
		if (overlays.contains(infoOverlay)) {
			int index = overlays.indexOf(infoOverlay);
	    	overlays.remove(index);
    	}
		
		// Setup new info status overlay
		infoOverlay = new InfoOverlay(infoStatus);
		// Add to the map
		mapView.getOverlays().add(infoOverlay);
	}
	
	/**
	 * Get the <i>MapView</i> object.
	 * 
	 * @return the map view.
	 */
	public MapView getMapView() {
		return mapView;
	}
	
// ------------------ PRIVATE METHODS ---------------------
	
	/*
	 * Initialized the map layout.
	 */
	private void init() {
		
		// Default city position
		int defaultLatitude = Integer.parseInt(getContext().getString(R.string.default_latitude));
		int defaultLongitude = Integer.parseInt(getContext().getString(R.string.default_longitude));
		GeoPoint defaultCity = new GeoPoint(defaultLatitude, defaultLongitude);
		
		
		// Setup the map view
		String apiKey = getContext().getString(R.string.default_apikey);
		mapView = new MapView(getContext(), apiKey);
		mapView.setEnabled(true);
		mapView.setClickable(true);

		// Add the map view into this layout.
		addView(mapView);
		
        // Using mapController to manipulate with map 
		MapController mapController = mapView.getController();
        mapController.setCenter(defaultCity);
        mapController.setZoom(15);
	}
	
}
